package org.shock.ss.sms.commons.listeners;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.struts2.ServletActionContext;

import org.shock.ss.sms.bean.system.UserInfor;

public class OnlineListener implements ServletContextListener,
		ServletContextAttributeListener, HttpSessionListener,
		HttpSessionAttributeListener {

	private ServletContext application = null;

	private Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
	private Map<String, HttpSession> sessionMapIP = new HashMap<String, HttpSession>();
	private Map<String, Boolean> cfdlUserMap = new HashMap<String, Boolean>();
	private Map<String, Boolean> cfdlIpMap = new HashMap<String, Boolean>();

	public void contextInitialized(ServletContextEvent sce) {

		// 初始化一个application对象

		this.application = sce.getServletContext();

		// 设置一个列表属性，用于保存在线用户名
		this.application.setAttribute("online", new ArrayList<UserInfor>());
		// 设置一个sessionMap属性，用于保存session
		this.application.setAttribute("sessionMap", sessionMap);
		this.application.setAttribute("sessionMapIP", sessionMapIP);
		this.application.setAttribute("cfdlUserMap", cfdlUserMap);
		this.application.setAttribute("cfdlIpMap", cfdlIpMap);

	}

	public void contextDestroyed(ServletContextEvent sce) {

	}

	public void attributeAdded(ServletContextAttributeEvent scab) {

	}

	public void attributeRemoved(ServletContextAttributeEvent scab) {

	}

	public void attributeReplaced(ServletContextAttributeEvent scab) {

	}

	public void sessionCreated(HttpSessionEvent se) {

	}

	@SuppressWarnings("unchecked")
	public void sessionDestroyed(HttpSessionEvent se) {

		// 取得用户名列表

		List<UserInfor> online = (List<UserInfor>) this.application.getAttribute("online");

		// 取得当前用户名

		UserInfor userinfor = (UserInfor) se.getSession().getAttribute(
				"userinfor");

		// 将此用户名从列表中删除
		online.remove(userinfor);
		// 将删除后的列表重新设置到application属性中
		this.application.setAttribute("online", online);
		if (null!=userinfor&&null != sessionMap.get(userinfor.getYgzh())) {
			sessionMap.remove(userinfor.getYgzh());
			sessionMapIP.remove(ServletActionContext.getRequest().getRemoteAddr());
		}
		this.application.setAttribute("sessionMap", sessionMap);
		this.application.setAttribute("sessionMapIP", sessionMapIP);
	}

	@SuppressWarnings("unchecked")
	public void attributeAdded(HttpSessionBindingEvent se) {
		HttpSession session = se.getSession();
		// 取得用户名列表
		List<UserInfor> online = (List<UserInfor>) this.application.getAttribute("online");
		UserInfor userinfor = (UserInfor) se.getValue();
		// 将当前用户名添加到列表中
		if (!online.contains(userinfor)) {
			online.add(userinfor);
		}
		// 将添加后的列表重新设置到application属性中
		this.application.setAttribute("online", online);
		sessionMap.put(userinfor.getYgzh(), session);
		sessionMapIP.put(ServletActionContext.getRequest().getRemoteAddr(), session);
		this.application.setAttribute("sessionMap", sessionMap);
		this.application.setAttribute("sessionMapIP", sessionMapIP);
		System.out.println("用户：" + userinfor.getYgxm() + "登录！");
		System.out.println("共计：" + online.size());
	}

	public void attributeRemoved(HttpSessionBindingEvent se) {

	}

	public void attributeReplaced(HttpSessionBindingEvent se) {

	}

}